package com.tgky.ybzb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tgky.ybzb.common.IdParam;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.entity.Jcybj;
import com.tgky.ybzb.mapper.JcybjMapper;
import com.tgky.ybzb.service.IJcybjService;
import com.tgky.ybzb.utils.MPUtil;
import com.tgky.ybzb.entity.Jcyb;
import com.tgky.ybzb.mapper.JcybMapper;
import com.tgky.ybzb.service.IJcybService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * 决策样本表 服务实现类
 *
 * @author XYP
 * @since 2024-07-22
 */
@Service
public class JcybServiceImpl extends ServiceImpl<JcybMapper, Jcyb> implements IJcybService {
    @Resource
    JcybMapper jcybMapper;

    /**
     * 通过实体类获取QueryWrapper
     * @param pageParam 分页参数
     * @param jcyb 实体信息
     * @return
     */
    @Override
    public QueryWrapper<Jcyb> getQueryWrapper(PageParam pageParam, Jcyb jcyb){
        QueryWrapper<Jcyb> queryWrapper = MPUtil.getQueryWrapper(pageParam);

        if(jcyb == null){
            return queryWrapper;
        }

        //主键
        if(jcyb.getId() != null){
            queryWrapper.lambda()
                    .eq(Jcyb::getId, jcyb.getId());
            return queryWrapper;
        }

        //TODO 此处可以根据各字段查询需求修改查询条件，eq、like、ge、gt、le、lt、ne...等等等
        queryWrapper.lambda()
//                .eq(jcyb.getJcybid() != null, Jcyb::getJcybid, jcyb.getJcybid())
                .eq(jcyb.getRwlx() != null, Jcyb::getRwlx, jcyb.getRwlx())
                .eq(jcyb.getMbxt() != null, Jcyb::getMbxt, jcyb.getMbxt())
                .eq(jcyb.getQxtj() != null, Jcyb::getQxtj, jcyb.getQxtj())
                .eq(jcyb.getGjys() != null, Jcyb::getGjys, jcyb.getGjys())
                .eq(jcyb.getSfdcjm() != null, Jcyb::getSfdcjm, jcyb.getSfdcjm())
                .eq(jcyb.getSfhzmb() != null, Jcyb::getSfhzmb, jcyb.getSfhzmb())
                .eq(jcyb.getZhlx() != null, Jcyb::getZhlx, jcyb.getZhlx())
                .eq(jcyb.getGzms() != null, Jcyb::getGzms, jcyb.getGzms())
                .eq(jcyb.getFbl() != null, Jcyb::getFbl, jcyb.getFbl())
                .eq(jcyb.getYxj() != null, Jcyb::getYxj, jcyb.getYxj())
                .eq(jcyb.getMbjd() != null, Jcyb::getMbjd, jcyb.getMbjd())
                .eq(jcyb.getMbwd() != null, Jcyb::getMbwd, jcyb.getMbwd())
                .eq(jcyb.getJcybjId() != null, Jcyb::getJcybjId, jcyb.getJcybjId());
        return queryWrapper;
    }

    /**
     * 保存样本数据并统计样本数量
     * @param jcyb
     * @return
     */
    @Override
    public boolean saveJcybAndCount(Jcyb jcyb) {
        int status=jcybMapper.insert(jcyb);
        updateJcybjCount(jcyb);
        return status==1 ? true:false;
    }

    /**
     * 删除样本并并更新样本集数量
     * @param idParam
     * @return
     */
    @Transactional
    @Override
    public boolean removeAndCount(IdParam<Long> idParam) {
        Jcyb jcyb=jcybMapper.selectById(idParam.getIds().get(0));
        int status=jcybMapper.deleteBatchIds(idParam.getIds());
        // 更新样本总数
        updateJcybjCount(jcyb);
        return status==1 ? true:false;
    }

    /**
     * 更新决策样本集数量
     * @param jcyb
     */
    public synchronized void updateJcybjCount(Jcyb jcyb){
        // 统计决策样本数量
        QueryWrapper<Jcyb> jcybQueryWrapper=MPUtil.getQueryWrapper();
        jcybQueryWrapper.eq("jcybj_id",jcyb.getJcybjId());
        Long count=jcybMapper.selectCount(jcybQueryWrapper);
        //更新决策样本集数量
        jcybMapper.updateJcybjsl(jcyb.getJcybjId(),count);
    }
}

